package com.songlanyun.modules.rabbitmq.config;

import com.songlanyun.modules.rabbitmq.DelayedConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMqDelayedConfig {


    //region 支付过期延
    /**
     * 初始化支付过期延时队列
     *
     * @return
     */
    @Bean
    public Queue orderDelayedQueue() {
        return new Queue(DelayedConstant.OrderDelayedConstant.ORDER_DELAYED_QUEUE);
    }

    /**
     * 定义一个支付过期延迟交换机
     *
     * @return
     */
    @Bean
    public CustomExchange orderDelayExchange() {
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(DelayedConstant.OrderDelayedConstant.ORDER_DELAYED_EXCHANGE, "x-delayed-message", true, false, args);
    }

    /**
     * 绑定队列到这个延迟交换机上
     *
     * @param queue
     * @param customExchange
     * @return
     */
    @Bean
    public Binding bindingNotify(@Qualifier("orderDelayedQueue") Queue queue,
                                 @Qualifier("orderDelayExchange") CustomExchange customExchange) {
        return BindingBuilder.bind(queue).to(customExchange).with(DelayedConstant.OrderDelayedConstant.ORDER_DELAYED_ROUTING).noargs();
    }

    //endregion

    //region  普通消费自提订单,执行自动备货
    /**
     * 普通消费自提订单,执行自动备货 对列
     * @return
     */
    @Bean
    public Queue autoStockUpDelayedQueue() {
        return new Queue(DelayedConstant.OrderAutoStockUpConstant.AUTO_STOCK_UP_DELAYED_QUEUE);
    }


    /**
     * 定义一个自动备货延迟交换机
     *
     * @return
     */
    @Bean
    public CustomExchange autoStockUpDelayExchange() {
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(DelayedConstant.OrderAutoStockUpConstant.AUTO_STOCK_UP_DELAYED_EXCHANGE, "x-delayed-message", true, false, args);
    }

    /**
     * 绑定队列到这个延迟交换机上
     * @param queue
     * @param customExchange
     * @return
     */
    @Bean
    public Binding bindingAutoStockUpNotify(@Qualifier("autoStockUpDelayedQueue") Queue queue,
                                 @Qualifier("autoStockUpDelayExchange") CustomExchange customExchange) {
        return BindingBuilder.bind(queue).to(customExchange).with(DelayedConstant.OrderAutoStockUpConstant.AUTO_STOCK_UP_DELAYED_ROUTING).noargs();
    }
    //endregion

    //region  普通消费物流订单,执行自动收货
    /**
     * 普通消费物流订单,执行自动收货 对列
     * @return
     */
    @Bean
    public Queue autoReceiptDelayedQueue() {
        return new Queue(DelayedConstant.OrderAutoReceiptConstant.AUTO_RECEIPT_DELAYED_QUEUE);
    }


    /**
     * 定义普通消费物流订单,执行自动收货延迟交换机
     *
     * @return
     */
    @Bean
    public CustomExchange autoReceiptDelayExchange() {
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(DelayedConstant.OrderAutoReceiptConstant.AUTO_RECEIPT_DELAYED_EXCHANGE, "x-delayed-message", true, false, args);
    }

    /**
     * 绑定队列到这个延迟交换机上
     * @param queue
     * @param customExchange
     * @return
     */
    @Bean
    public Binding bindingAutoReceiptNotify(@Qualifier("autoReceiptDelayedQueue") Queue queue,
                                            @Qualifier("autoReceiptDelayExchange") CustomExchange customExchange) {
        return BindingBuilder.bind(queue).to(customExchange).with(DelayedConstant.OrderAutoReceiptConstant.AUTO_RECEIPT_DELAYED_ROUTING).noargs();
    }
    //endregion

    //region 普通自提订单,备货完成后，买家过期不来拿货，自动完成
    /**
     * 普通自提订单,备货完成后，买家过期不来拿货，自动完成 对列
     * @return
     */
    @Bean
    public Queue autoPickupDelayedQueue() {
        return new Queue(DelayedConstant.OrderAutoPickupConstant.AUTO_PICKUP_DELAYED_QUEUE);
    }


    /**
     * 定义 普通自提订单,备货完成后，买家过期不来拿货，自动完成 延迟交换机
     *
     * @return
     */
    @Bean
    public CustomExchange autoPickupDelayExchange() {
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(DelayedConstant.OrderAutoPickupConstant.AUTO_PICKUP_DELAYED_EXCHANGE, "x-delayed-message", true, false, args);
    }

    /**
     * 绑定队列到这个延迟交换机上
     * @param queue
     * @param customExchange
     * @return
     */
    @Bean
    public Binding bindingAutoPickupNotify(@Qualifier("autoPickupDelayedQueue") Queue queue,
                                            @Qualifier("autoPickupDelayExchange") CustomExchange customExchange) {
        return BindingBuilder.bind(queue).to(customExchange).with(DelayedConstant.OrderAutoPickupConstant.AUTO_PICKUP_DELAYED_ROUTING).noargs();
    }
    //endregion

    //region 维权申请商家过期没处理自动同意
    /**
     * 维权申请商家过期没处理自动同意 对列
     * @return
     */
    @Bean
    public Queue rightsAutoAgreeDelayedQueue() {
        return new Queue(DelayedConstant.RightsAutoAgreeConstant.RIGHTS_AUTO_AGREE_DELAYED_QUEUE);
    }


    /**
     * 定义 维权申请商家过期没处理自动同意 延迟交换机
     *
     * @return
     */
    @Bean
    public CustomExchange rightsAutoAgreeDelayExchange() {
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(DelayedConstant.RightsAutoAgreeConstant.RIGHTS_AUTO_AGREE_DELAYED_EXCHANGE, "x-delayed-message", true, false, args);
    }

    /**
     * 绑定队列到这个延迟交换机上
     * @param queue
     * @param customExchange
     * @return
     */
    @Bean
    public Binding bindingRightsAutoAgreeNotify(@Qualifier("rightsAutoAgreeDelayedQueue") Queue queue,
                                           @Qualifier("rightsAutoAgreeDelayExchange") CustomExchange customExchange) {
        return BindingBuilder.bind(queue).to(customExchange).with(DelayedConstant.RightsAutoAgreeConstant.RIGHTS_AUTO_AGREE_DELAYED_ROUTING).noargs();
    }
    //endregion

    //region 商家同意之后，买家过期没有填写物流单，需要自动取消的维权单（退货单）
    /**
     * 商家同意之后，买家过期没有填写物流单，需要自动取消的维权单（退货单） 对列
     * @return
     */
    @Bean
    public Queue rightsAutoCancelDelayedQueue() {
        return new Queue(DelayedConstant.RightsAutoCancelConstant.RIGHTS_AUTO_CANCEL_DELAYED_QUEUE);
    }


    /**
     * 定义 商家同意之后，买家过期没有填写物流单，需要自动取消的维权单（退货单） 延迟交换机
     * @return
     */
    @Bean
    public CustomExchange rightsAutoCancelDelayExchange() {
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(DelayedConstant.RightsAutoCancelConstant.RIGHTS_AUTO_CANCEL_DELAYED_EXCHANGE, "x-delayed-message", true, false, args);
    }

    /**
     * 绑定队列到这个延迟交换机上
     * @param queue
     * @param customExchange
     * @return
     */
    @Bean
    public Binding bindingRightsAutoCancelNotify(@Qualifier("rightsAutoCancelDelayedQueue") Queue queue,
                                                @Qualifier("rightsAutoCancelDelayExchange") CustomExchange customExchange) {
        return BindingBuilder.bind(queue).to(customExchange).with(DelayedConstant.RightsAutoCancelConstant.RIGHTS_AUTO_CANCEL_DELAYED_ROUTING).noargs();
    }
    //endregion

    //region 需要自动收货的维权单（退货单）
    /**
     * 需要自动收货的维权单（退货单） 对列
     * @return
     */
    @Bean
    public Queue rightsAutoReceiptDelayedQueue() {
        return new Queue(DelayedConstant.RightsAutoReceiptConstant.RIGHTS_AUTO_RECEIPT_DELAYED_QUEUE);
    }


    /**
     * 定义 需要自动收货的维权单（退货单） 延迟交换机
     * @return
     */
    @Bean
    public CustomExchange rightsAutoReceiptDelayExchange() {
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(DelayedConstant.RightsAutoReceiptConstant.RIGHTS_AUTO_RECEIPT_DELAYED_EXCHANGE, "x-delayed-message", true, false, args);
    }

    /**
     * 绑定队列到这个延迟交换机上
     * @param queue
     * @param customExchange
     * @return
     */
    @Bean
    public Binding bindingRightsAutoReceiptNotify(@Qualifier("rightsAutoReceiptDelayedQueue") Queue queue,
                                                 @Qualifier("rightsAutoReceiptDelayExchange") CustomExchange customExchange) {
        return BindingBuilder.bind(queue).to(customExchange).with(DelayedConstant.RightsAutoReceiptConstant.RIGHTS_AUTO_RECEIPT_DELAYED_ROUTING).noargs();
    }
    //endregion

    //region 子订单自动封单
    /**
     * 子订单自动封单 对列
     * @return
     */
    @Bean
    public Queue autoEndChildOrderDelayedQueue() {
        return new Queue(DelayedConstant.ChildOrderAutoEndConstant.CHILD_ORDER_AUTO_END_DELAYED_QUEUE);
    }


    /**
     * 定义 子订单自动封单 延迟交换机
     * @return
     */
    @Bean
    public CustomExchange autoEndChildOrderDelayExchange() {
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(DelayedConstant.ChildOrderAutoEndConstant.CHILD_ORDER_AUTO_END_DELAYED_EXCHANGE, "x-delayed-message", true, false, args);
    }

    /**
     * 绑定队列到这个延迟交换机上
     * @param queue
     * @param customExchange
     * @return
     */
    @Bean
    public Binding bindingAutoEndChildOrderNotify(@Qualifier("autoEndChildOrderDelayedQueue") Queue queue,
                                                  @Qualifier("autoEndChildOrderDelayExchange") CustomExchange customExchange) {
        return BindingBuilder.bind(queue).to(customExchange).with(DelayedConstant.ChildOrderAutoEndConstant.CHILD_ORDER_AUTO_END_DELAYED_ROUTING).noargs();
    }
    //endregion

    //region 子订单自动评论
    /**
     * 子订单自动评论 对列
     * @return
     */
    @Bean
    public Queue autoCommentChildOrderDelayedQueue() {
        return new Queue(DelayedConstant.ChildOrderAutoCommentConstant.CHILD_ORDER_AUTO_COMMENT_DELAYED_QUEUE);
    }


    /**
     * 定义 子订单自动评论 延迟交换机
     * @return
     */
    @Bean
    public CustomExchange autoCommentChildOrderDelayExchange() {
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(DelayedConstant.ChildOrderAutoCommentConstant.CHILD_ORDER_AUTO_COMMENT_DELAYED_EXCHANGE, "x-delayed-message", true, false, args);
    }

    /**
     * 绑定队列到这个延迟交换机上
     * @param queue
     * @param customExchange
     * @return
     */
    @Bean
    public Binding bindingAutoCommentChildOrderNotify(@Qualifier("autoCommentChildOrderDelayedQueue") Queue queue,
                                                  @Qualifier("autoCommentChildOrderDelayExchange") CustomExchange customExchange) {
        return BindingBuilder.bind(queue).to(customExchange).with(DelayedConstant.ChildOrderAutoCommentConstant.CHILD_ORDER_AUTO_COMMENT_DELAYED_ROUTING).noargs();
    }
    //endregion

    //region 过期没有成团的团需要取消
    /**
     * 过期没有成团的团需要取消 对列
     * @return
     */
    @Bean
    public Queue autoExpireTeamDelayedQueue() {
        return new Queue(DelayedConstant.AutoExpireTeamConstant.AUTO_EXPIRE_TEAM_DELAYED_QUEUE);
    }


    /**
     * 定义 子订单自动评论 延迟交换机
     * @return
     */
    @Bean
    public CustomExchange autoExpireTeamDelayExchange() {
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(DelayedConstant.AutoExpireTeamConstant.AUTO_EXPIRE_TEAM_DELAYED_EXCHANGE, "x-delayed-message", true, false, args);
    }

    /**
     * 绑定队列到这个延迟交换机上
     * @param queue
     * @param customExchange
     * @return
     */
    @Bean
    public Binding bindingAutoExpireTeamNotify(@Qualifier("autoExpireTeamDelayedQueue") Queue queue,
                                                      @Qualifier("autoExpireTeamDelayExchange") CustomExchange customExchange) {
        return BindingBuilder.bind(queue).to(customExchange).with(DelayedConstant.AutoExpireTeamConstant.AUTO_EXPIRE_TEAM_DELAYED_ROUTING).noargs();
    }
    //endregion
}
